package com.nulldev.util.other;

import java.text.DecimalFormat;

public class FPSCounter extends Timer{

	private double fps, time;
	private double avg, avgTime;
	private long ticks;
	
	public FPSCounter() {
		super();
		this.fps = 0.0;
		this.UPDATE_RATE = 1000L;
	}
	
	@Override
	void updateF() {
		if (this.ticks > 0) {
			if (this.avg <= 0) {
				this.avg = this.ticks;
			}else {
				this.avg = (this.fps + this.ticks) / 2;
			}
			
			if (this.avgTime <= 0) {
				this.avgTime = this.time;
			}else {
				this.avgTime = (this.avgTime + this.time) / 2;
			}
			this.fps = this.ticks;
			this.ticks = 0;
		}
	}
	
	public void callFPS() {
		this.ticks += 1;
	}
	
	public double getFPS() {
		return this.fps;
	}
	
	public double getAVGFPS() {
		return this.avg;
	}

	private long startTime, endTime;
	public void timeStart() {
		this.startTime = System.currentTimeMillis();
	}
	
	public void timeEnd() {
		this.endTime = System.currentTimeMillis();
		this.time = this.endTime - this.startTime;
	}
	
	public double avgRenderTime() {
		return this.avgTime;
	}
	
	private final DecimalFormat format = new DecimalFormat("##.#");
	public String avgRenderTimeS() {
		return this.format.format(this.avgTime);
	}
}
